<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="demo">
        <p>firstName: <input type="text" name="" v-model="firstName"></p>
        <p>lastName: <input type="text" name="" v-model="lastName"></p>
        <p>fullName: {{fullName}}</p>
        <p>fullName: {{fullName}}</p>
        <p>fullName: {{fullName}}</p>
    </div>
    <script>
        Vue.config.productionTip = false
        new Vue({
            el: '#demo',
            data: {
                firstName:'zhang',
                lastName:'san',
                fullName:''
            },
            watch:{
                firstName:{
                    immediate:true,
                    handler(value){
                        /**
                         * 定时器的回调函数是window调用的，所以内部的this指向window，因此在vue中，vue管理的函数要定义成普通函数，确保this指向当前实例。一切其他的回调函数[定时器，其他异步回调],如果想在内部通过this获取当前组件实例，需要定义成箭头函数
                         */
                        setTimeout(()=>{
                            this.fullName = value + '-' + this.lastName
                        },2000)
                    }
                },
                lastName:{
                    immediate:true,
                    handler(value){
                        this.fullName = this.firstName + '-' + value
                    }
                }
            }
        })
    </script>
</body>

</html>